﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

namespace DigDes.DSchool.SUPS.WebApp
{
    public partial class CargoForm : System.Web.UI.Page
    {
        protected const int columnNumber = 9;

        protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 0; i <= columnNumber; i++)
                GridView1.HeaderRow.Cells[i].CssClass = "grid-header-cell";
            ChangeMod(false);

        }

        protected void PageIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i <= columnNumber; i++)
                GridView1.HeaderRow.Cells[i].CssClass = "grid-header-cell";
            GridView1.DataBind();
            // GridViewPanel.Update();

        }

        protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {

                if (row.RowIndex == GridView1.SelectedIndex)
                {
                    row.CssClass = "grid-selected-row";
                    row.ToolTip = string.Empty;

                }
                else
                {
                    row.CssClass = "grid-row";
                    row.ToolTip = "Нажмите,чтобы выбрать";
                }
            }
            RowDetails.Visible = true;
            DVNameLabel.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
            RowDetails.ChangeMode(FormViewMode.ReadOnly);
            ChangeMod(false);
            Edit_Button.Visible = true;
            Delete_Button.Visible = true;
            //GridView1.DataBind();
        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            }
        }
        protected void Delete_Button_Click(Object sender, EventArgs e)
        {
            // Programmatically delete the selected record.
            GridView1.DeleteRow(GridView1.SelectedIndex);
        }

        protected void Add_Button_Click(object sender, EventArgs e)
        {
            DVNameLabel.Text = "Добавление нового груза";
            RowDetails.Visible = true;
            ChangeMod(true);
            RowDetails.ChangeMode(FormViewMode.Insert);

        }



        protected void Edit_Button_Click(object sender, EventArgs e)
        {
            RowDetails.ChangeMode(FormViewMode.Edit);
            //Bind the DetailsView with data 
            RowDetails.DataBind();
            if (GridView1.SelectedIndex != -1)
            {
                ChangeMod(true);

                DVNameLabel.Text = "Редактирование : " + GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
                Save_Button.BackColor = ColorTranslator.FromHtml("#5E9BC4");
            }

        }

        protected void Cancel_Button_Click(object sender, EventArgs e)
        {
            RowDetails.ChangeMode(FormViewMode.ReadOnly);
            ChangeMod(false);
            RowDetails.DataBind();
            DVNameLabel.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
        }

        protected void ChangeMod(bool mod)
        {
            Cancel_Button.Visible = mod;
            Save_Button.Visible = mod;
            Image_Upload.Visible = mod;
            Load_Button.Visible = mod;
        }

        protected void Save_Button_Click(object sender, EventArgs e)
        {
            if (RowDetails.CurrentMode == FormViewMode.Edit)
            {
                RowDetails.UpdateItem(true);
                GridView1.DataBind();
                DVNameLabel.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
            }
            else
            {
                RowDetails.InsertItem(true);
                RowDetails.ChangeMode(FormViewMode.ReadOnly);
            }
            ChangeMod(false);
            //GridView1.DataSource
        }


        protected void Load_Button_Click(object sender, EventArgs e)
        {

            DigDes.DSchool.SUPS.DataAccess.Database.CargoWebFormLoader.UploadImageByID(((TextBox)RowDetails.FindControl("Cargo_ID")).Text, Image_Upload.FileBytes);

        }

    }
}